package com.mengwen.backstage.controller.partner;

import com.mengwen.backstage.config.WeChatConfig;
import com.mengwen.backstage.pojo.ProfitRate;
import com.mengwen.backstage.pojo.UpdateBalanceDto;
import com.mengwen.backstage.service.InvitationService;
import com.mengwen.backstage.service.PartnerService;
import com.mengwen.backstage.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Lock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LiangYonghui
 * @date 2020/4/17 15:29
 * @description  管理员的余额管理
 */
@Controller
@Slf4j
@Transactional
public class PartnerBalanceController {

    @Autowired
    WeChatConfig weChatConfig;

    @Autowired
    InvitationService invitationService;

    @Autowired
    PartnerService  partnerService;


    /**
     * 根据被邀请人本次发布问卷的金额的0.03更新
     * @param updateBalanceDto
     *
     * 接口传参：
     * {
     * 			"money": "100",
     * 			"unionid": "1",
     * 			"updateMoneyToken" : "11"
     *
     * }
     */
    @RequestMapping("partner/publish")
    public void publishQuestion(@RequestBody UpdateBalanceDto updateBalanceDto, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String unionid = (String) claims.get("unionid");
        String type = (String) claims.get("type");

        // 校验权限pc端的token，看是否具有权限
        if (type.equals("type") && ! (null == unionid) ) {
            // 根据被邀请者的unionid查询邀请者的unionid
            String inviterUnionid = invitationService.findInviterUnionidByInviteeUnionid(unionid);
            if(inviterUnionid == null){
                log.info("被邀请人："+ unionid + "未找到对应的邀请人");
                return;
            }else {
                // 更新金额：0.03利润
                Double money = updateBalanceDto.getMoney();

                if( (money > 500) || (money < 0) || (money == null) ) {
                    log.info("金额不符合规范，金额可能遭篡改");
                }

                money = money * ProfitRate.ZeroPointZeroThree;

                Integer count = partnerService.updateMoneyByunionid(inviterUnionid,money);

                if ( count!= 1) {
                    log.info("提现更新金额失败：unionid：" +  inviterUnionid);
                }else {
                    log.info("提现金额更新成功 unionid:" + inviterUnionid);
                }

            }
        }

    }




    /**
     * 根据被邀请人本次回答问卷的金额的0.03更新
     * @param updateBalanceDto
     */
    @RequestMapping("partner/answer")
    public void answerQuestion(@RequestBody UpdateBalanceDto updateBalanceDto, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        String unionid = (String) claims.get("unionid");
        String type = (String) claims.get("type");

        // 校验权限pc端的token，看是否具有权限
        if (type.equals("type") && ! (null == unionid) ) {
            // 根据被邀请者的unionid查询邀请者的unionid
            String inviterUnionid = invitationService.findInviterUnionidByInviteeUnionid(unionid);
            if(inviterUnionid == null){
                log.info("被邀请人："+ unionid + "未找到对应的邀请人");
                return;
            }else {
                // 更新金额：0.02利润
                Double money = updateBalanceDto.getMoney();

                if( (money > 500) || (money < 0) || (money == null) ) {
                    log.info("金额不符合规范，金额可能遭篡改");
                }

                money = money * ProfitRate.ZeroPointZeroTwo;

                Integer count = partnerService.updateMoneyByunionid(inviterUnionid,money);

                if ( count!= 1) {
                    log.info("提现更新金额失败：unionid：" +  inviterUnionid);
                }else {
                    log.info("提现金额更新成功 unionid:" + inviterUnionid);
                }

            }
        }

    }


}
